using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Trang;

namespace ArchSoft
{
    public class DwgFileTool
    {
        
        public static void CreateDwgFile( string fileName )
        {
            if( AutocadHelper.Instance.TemplateFileForCreating != string.Empty )
                System.IO.File.Copy( AutocadHelper.Instance.TemplateFileForCreating, fileName );
        }

        public static void CreateDwgFile(string fileName,string DefaultName)
        {
            try
            {
                if (File.Exists(DefaultName))
                    System.IO.File.Copy(DefaultName, fileName);
            }
            catch { }
        }

        public static string GetNewFile(int windowPos, string path )
        {
            int filePos = 1;
            string fileName;
            do
            {
                fileName = CodeNameUtilities.GenerateDrawingName(windowPos, filePos, "*");
                filePos++;
            }
            while (System.IO.Directory.GetFiles(path, fileName,SearchOption.TopDirectoryOnly).GetLength(0)>0);

            return fileName.Replace("*", "Drawing");
        }


        public static string GetNewFileOld(System.Collections.Hashtable ht, int windowPos)
        {
            int filePos = 1;
            string fileName = string.Empty;
            do
            {
                if (windowPos == -1)
                    fileName = string.Format("{1:00}_Drawing" + Constants.DWG_EXT_FILE, windowPos, filePos);
                else
                    fileName = string.Format("A{0}-{1:00}_Drawing" + Constants.DWG_EXT_FILE, windowPos, filePos);
                filePos++;
            }
            while (ht[fileName] != null);

            return fileName;
        }

        public static string GetNewFile( System.Collections.Hashtable ht, int windowPos)
        {
            int filePos = 1;
            string fileName = string.Empty;
            do
            {
                if (windowPos == -1)
                    fileName = string.Format(ProjectManager.PROJ_DEFAULT_CHAR + "_Drawing({1:0})" + Constants.DWG_EXT_FILE, windowPos, filePos);
                else
                    fileName = string.Format(ProjectManager.PROJ_DEFAULT_CHAR + "{0}-{1:00}_Drawing" + Constants.DWG_EXT_FILE, windowPos, filePos);
                filePos++;
            }
            while (ht[fileName] != null);

            return fileName;
        }

        public static string GetNewCopyFile( string path, string fileName )
        {
            string newFileName = "Copy of " + fileName;
            int pos = 2;
            while (System.IO.File.Exists(path + Path.DirectorySeparatorChar + newFileName))
            {
                newFileName = string.Format("Copy ({0}) {1}", pos++, fileName);
            }

            return newFileName;
        }

        public static string GetNewCopyFile(System.Collections.Hashtable ht, string fileName)
        {
            string newFileName = "Copy of " + fileName;
            int pos = 2;
            while (ht[newFileName] != null)
            {
                newFileName = string.Format("Copy ({0}) {1}", pos++, fileName);
            }

            return newFileName;
        }

        public static string GetIssuedDirectory( string path )
        {
            int filePos = 2;
            string dateTimeString = DateTime.Now.ToString("yyMMdd");
            string newDirectoryName = dateTimeString;
            while (System.IO.Directory.Exists(path + Path.DirectorySeparatorChar + newDirectoryName))
            {
                newDirectoryName = string.Format("{0}({1})", dateTimeString, filePos++);
            }

            return newDirectoryName;
        }

        public static void CopyDirectory(string src, string dst)
        {
            CopyDirectory(src,dst,null);
        }

        public static void CopyDirectory(string src, string dst,string newName) 
        {
            String[] files; 
            if(dst[dst.Length-1] != Path.DirectorySeparatorChar) 
                dst+=Path.DirectorySeparatorChar; 
            
            if(!Directory.Exists(dst)) Directory.CreateDirectory(dst); 
                files=Directory.GetFileSystemEntries(src); 
            foreach(string element in files)
            {
                // Sub directories 
                if (Directory.Exists(element))
                    CopyDirectory(element, dst + Path.GetFileName(element));
                // files in directory 
                else
                {
                    if (newName != null)
                    {
                        File.Copy(element, dst + newName + Path.GetExtension(element), true);
                    }
                    else
                    {
                        File.Copy(element, dst + Path.GetFileName(element), true);
                    }
                    
                }
            } 
        }
    }
}
